﻿<?php
require_once "config.php";
require_once "OpenApiV3.php";
require_once "server.class.php";
require_once "db.class.php";

// 用户的OpenID/OpenKey
$openid = $_POST['openid'];
$openkey = $_POST['openkey'];
$pf = $_POST['pf'];


$sdk = new OpenApiV3($appid, $appkey);
$sdk->setServerName($server_name);

$qqRet = get_user_info($sdk, $openid, $openkey, $pf);

$xmlContent = "";

if ($qqRet['ret'] == 0)
{
	$params = array("host"=>"$host", "user"=>"$user", "pass"=>"$pass", "db"=>"$db");
	$db = new Dbclass($params, false);
	$server = new ServerHelperClass($db, $tables);

	$sql = "select id from ".$tb_user_profile." where openId = '".$openid."'";
	$result = $db->query($sql);
	//用户已经存在
	if ($arr = $db->fetchArray($result))
	{
		$userId = $arr['id'];
		//删除call id历史
		$sql = "delete from ".$tb_user_call." where ownerId = ".$userId;
		$result = $db->query($sql);
	}
	//用户未被创建
	else
	{
		$userId = $server->createUser($openid, $qqRet['nickname']);
	}
	//读取用户信息
	$sql = "select campTankId, openId, logonDayCount, lastLogonDate, finishGuide, fetchGiftDate, fetchNewPlayerGift, musicVolume, effectVolume, currentLevel from ".$tb_user_profile." a inner join ".$tb_system_profile." b on a.id = b.ownerId where id = ".$userId;
	$result = $db->query($sql);
	if ($arr = $db->fetchArray($result))
	{
		list($usec, $sec) = explode(" ", microtime()); 
		$sessionId = $sec.($usec * 1000000);
		$lastDay = floor($arr['lastLogonDate'] / 86400);
		$today = floor($timeStamp / 86400);
		$logonDayCount = $arr['logonDayCount'];
		if ($today == $lastDay + 1)
		{
			$logonDayCount++;
			$sql = "update ".$tb_user_profile." set logonDayCount = ".$logonDayCount.", lastLogonDate = ".$timeStamp.", sessionId = '".$sessionId."' where id = ".$userId;
			$db->query($sql);
		}
		else if ($today > $lastDay + 1)
		{
			$logonDayCount = 1;
			$sql = "update ".$tb_user_profile." set logonDayCount = ".$logonDayCount.", lastLogonDate = ".$timeStamp.", sessionId = '".$sessionId."' where id = ".$userId;
			$db->query($sql);
		}
		else
		{
			$sql = "update ".$tb_user_profile." set sessionId = '".$sessionId."' where id = ".$userId;
			$db->query($sql);
		}
		$xmlContent .= "<sessionId><![CDATA[".$sessionId."]]></sessionId>\n";
		$xmlContent .= "<musicVolume><![CDATA[".$arr['musicVolume']."]]></musicVolume>\n";
		$xmlContent .= "<effectVolume><![CDATA[".$arr['effectVolume']."]]></effectVolume>\n";
		$xmlContent .= "<playerInfo>\n";
		$xmlContent .= "<playerId><![CDATA[".$userId."]]></playerId>\n";
		$xmlContent .= "<campTankId><![CDATA[".$arr['campTankId']."]]></campTankId>\n";
		$xmlContent .= "<openId><![CDATA[".$arr['openId']."]]></openId>\n";
		$xmlContent .= "<nickname><![CDATA[".$qqRet['nickname']."]]></nickname>\n";
		$xmlContent .= "<is_yellow_vip><![CDATA[".$qqRet['is_yellow_vip']."]]></is_yellow_vip>\n";
		$xmlContent .= "<is_yellow_year_vip><![CDATA[".$qqRet['is_yellow_year_vip']."]]></is_yellow_year_vip>\n";
		$xmlContent .= "<yellow_vip_level><![CDATA[".$qqRet['yellow_vip_level']."]]></yellow_vip_level>\n";
		$xmlContent .= "<figureurl><![CDATA[".$qqRet['figureurl']."]]></figureurl>\n";
		$xmlContent .= "<logonDayCount><![CDATA[".$logonDayCount."]]></logonDayCount>\n";
		$xmlContent .= "<finishGuide><![CDATA[".$arr['finishGuide']."]]></finishGuide>\n";
		$xmlContent .= "<fetchGiftDate><![CDATA[".$arr['fetchGiftDate']."]]></fetchGiftDate>\n";
		$xmlContent .= "<fetchNewPlayerGift><![CDATA[".$arr['fetchNewPlayerGift']."]]></fetchNewPlayerGift>\n";
		$xmlContent .= "<currentLevel><![CDATA[".$arr['currentLevel']."]]></currentLevel>\n";
		$xmlContent .= "</playerInfo>\n";
	}
	$xmlContent .= $server->getTankList($userId);
	$xmlContent .= $server->getObjList($userId);
	$xmlContent .= $server->getTaskList($userId);
	$xmlContent .= $server->getCampList($userId);
	$xmlContent .= $server->getMailAndGiftList($userId);

	$db->close();
	$return = "success";
}
else
{
	$return = "fail";
	$return = $qqRet['msg'];
}

$txt = "";
$txt .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$txt .= "<values>\n";
$txt .= "<debug><![CDATA[".$debug."]]></debug>\n";
$txt .= "<action><![CDATA[loginMe]]></action>\n";
$txt .= "<result><![CDATA[".$return."]]></result>\n";
$txt .= "<timeStamp><![CDATA[".$timeStamp."]]></timeStamp>\n";
$txt .= $xmlContent;
$txt .= "</values>";

echo $txt;

/**
 * 获取好友资料
 *
 * @param object $sdk OpenApiV3 Object
 * @param string $openid openid
 * @param string $openkey openkey
 * @param string $pf 平台
 * @return array 好友资料数组
 */
function get_user_info(& $sdk, $openid, $openkey, $pf)
{
	$params = array(
		'openid' => $openid,
		'openkey' => $openkey,
		'pf' => $pf,
	);
	
	$script_name = '/v3/user/get_info';

	return $sdk->api($script_name, $params);
}

?>
